﻿using Domain.Entity;
using DtoFw;
using DtoFw.Enum;
using DtoFw.Interface;
using DtoFw.Query;
using DtoFw.Result;
using Repository.Base;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using Util;

namespace Repository
{
    public class CasePhysicalEvidenceRepository : BaseRepository<CasePhysicalEvidenceEntity>
    {
        private readonly SnowFlakeGenerator worker;
        public CasePhysicalEvidenceRepository(SnowFlakeGenerator snowFlakeGenerator) : base(snowFlakeGenerator)
        {
            this.worker = snowFlakeGenerator;
        }
        /// <summary>
        /// 案件物品分页
        /// </summary>
        /// <param name="reqDto"></param>
        /// <returns></returns>
        public Pager<CaseEvidencePagerRespDto> QueryCaseEvidences(EvidenceQueryReqDto reqDto)
        {
            int total = 0;
            Pager<CaseEvidencePagerRespDto> pager = new Pager<CaseEvidencePagerRespDto>();
            pager.Data = base.GetDb().Queryable<CaseEntity, CasePhysicalEvidenceEntity>((t1, t2) => new JoinQueryInfos(
                JoinType.Left, t1.Id == t2.CaseId))
                .Where((t1, t2)=>!t1.IsDeleted && !t2.IsDeleted && t1.OrganId == reqDto.OrganId )
                .WhereIF(reqDto.CaseId > 0, t1 => t1.Id == reqDto.CaseId)
                .WhereIF(!string.IsNullOrWhiteSpace(reqDto.CaseNo), t1 => t1.CaseNo.Contains(reqDto.CaseNo))
                .WhereIF(!string.IsNullOrWhiteSpace(reqDto.CaseName), t1 => t1.CaseName.Contains(reqDto.CaseName))
                .WhereIF(!string.IsNullOrWhiteSpace(reqDto.InspectionNo), t1 => t1.InspectionNo.Contains(reqDto.InspectionNo))
                .WhereIF(reqDto.CaseTimeStart.HasValue && reqDto.CaseTimeEnd.HasValue,
                    (t1,t2) => t1.CaseTime > reqDto.CaseTimeStart && t1.CaseTime <= reqDto.CaseTimeEnd)
                .WhereIF(reqDto.CreateEndDateTime.HasValue && reqDto.CreateStartDateTime.HasValue,
                    (t1,t2) => t2.CreateTime > reqDto.CreateStartDateTime && t2.CreateTime <= reqDto.CreateEndDateTime)
                .WhereIF(!string.IsNullOrWhiteSpace(reqDto.EvidenceNo), (t1, t2) => t2.No.Contains(reqDto.EvidenceNo))
                .WhereIF(!string.IsNullOrWhiteSpace(reqDto.EvidenceName), (t1, t2) => t2.Name.Contains(reqDto.EvidenceName))
                .WhereIF(reqDto.EvidenceStatus > -1, (t1, t2) => t2.Status == reqDto.EvidenceStatus)
                .WhereIF(!string.IsNullOrWhiteSpace(reqDto.PersonName), (t1, t2) => t2.ExtractionPersonName.Contains(reqDto.PersonName))
                .WhereIF(reqDto.ExtractionDateTimeStart.HasValue && reqDto.ExtractionDateTimeEnd.HasValue,(t1, t2) => t2.ExtractionDateTime > reqDto.ExtractionDateTimeStart && t2.ExtractionDateTime <= reqDto.ExtractionDateTimeEnd)
                .WhereIF(reqDto.CreateStartDateTime.HasValue && reqDto.CreateEndDateTime.HasValue, (t1, t2) => t2.CreateTime > reqDto.CreateStartDateTime && t2.CreateTime <= reqDto.CreateEndDateTime)
                 .OrderBy((t1, t2) => t1.CreateTime, OrderByType.Desc)
                .Select((t1, t2) => new CaseEvidencePagerRespDto()
                {
                    CaseId = t1.Id,
                    EvidenceId = t2.Id,
                    InspectionNo = t1.InspectionNo,
                    CaseName = t1.CaseName,
                    EvidenceNo = t2.No,
                    EvidenceName = t2.Name,
                    EvidenceQuantity = t2.Quantity,
                    EvidenceClass = t2.Class,
                    EvidenceCategory = t2.Category,
                    EvidenceSource = t2.Source,
                    ExtractionPersonName = t2.ExtractionPersonName,
                    ExtractionDateTime = t2.ExtractionDateTime,
                    Location = t2.Location,
                    Status = (EnumEvidenceStatus)t2.Status,
                    RFID = t2.RFID,
                    CaseNo = t1.CaseNo,
                    EvidenceClassName=t2.ClassName,
                    EvidenceCategoryName=t2.CategoryName,
                    EvidenceSourceName=t2.SourceName
                })
                .ToPageList(reqDto.Page, reqDto.PageSize, ref total);
            pager.TotalCount = total;
            return pager;
        }
        /// <summary>
        /// 查看箱子物品
        /// </summary>
        /// <param name="reqDto"></param>
        /// <returns></returns>
        public Pager<CaseEvidencePagerRespDto> QueryCaseEvidences(EvidenceLocationQueryReqDto reqDto)
        {
            int total = 0;
            Pager<CaseEvidencePagerRespDto> pager = new Pager<CaseEvidencePagerRespDto>();
            pager.Data = base.GetDb().Queryable<CaseEntity, CasePhysicalEvidenceEntity, CasePhysicalEvidenceTransfereEntity>((t1, t2, t3) => new JoinQueryInfos(
                  JoinType.Left, t1.Id == t2.CaseId, JoinType.Left, t2.Id == t3.EvidenceId))
                .Where((t1, t2, t3) => t1.IsDeleted == false && t2.IsDeleted == false && t3.IsDeleted == false)
                .WhereIF(reqDto.HistoryStatus == 0,(t1, t2, t3) => t3.HistoryStatus == 0 && t2.Status == 1)
                 .WhereIF(reqDto.HistoryStatus == 1, (t1, t2, t3) => t3.HistoryStatus == reqDto.HistoryStatus)
                .WhereIF(!string.IsNullOrWhiteSpace(reqDto.CaseNo), t1 => t1.CaseNo == reqDto.CaseNo)
                .WhereIF(!string.IsNullOrWhiteSpace(reqDto.InspectionNo), t1 => t1.InspectionNo == reqDto.InspectionNo)
                .WhereIF(!string.IsNullOrWhiteSpace(reqDto.EvidenceNo), (t1, t2, t3) => t2.No == reqDto.EvidenceNo)
                .WhereIF(reqDto.LocationId>0, (t1, t2, t3) => t3.LocationId == reqDto.LocationId)
                .OrderBy((t1, t2,t3) => t2.CreateTime, OrderByType.Desc)
                .Select((t1, t2, t3) => new CaseEvidencePagerRespDto()
                {
                    CaseId = t1.Id,
                    EvidenceId = t2.Id,
                    InspectionNo = t1.InspectionNo,
                    CaseName = t1.CaseName,
                    EvidenceNo = t2.No,
                    EvidenceName = t2.Name,
                    EvidenceQuantity = t2.Quantity,
                    EvidenceClass = t2.Class,
                    EvidenceCategory = t2.Category,
                    EvidenceSource = t2.Source,
                    ExtractionPersonName = t2.ExtractionPersonName,
                    ExtractionDateTime = t2.ExtractionDateTime,
                    Location = t2.Location,
                    Status = (EnumEvidenceStatus)t2.Status,
                    RFID = t2.RFID,
                    CaseNo = t1.CaseNo,
                    EvidenceClassName = t2.ClassName,
                    EvidenceCategoryName = t2.CategoryName,
                    EvidenceSourceName = t2.SourceName,
                    ExtractMethod=t2.ExtractMethod
                })
                .ToPageList(reqDto.Page, reqDto.PageSize, ref total);
            pager.TotalCount = total;
            return pager;
        }

        public void ModiftyNotificationHandler(long[] ids, string user,string ty)
        {
            string sql = $"update notification set IsHandle=1,HandlePerson='{user}',HandleTime=now() where CheckId in({string.Join(",", ids)}) and Influence='{ty}'";
            this.GetDb().Ado.ExecuteCommand(sql);
        }
        public bool DeleteTrans(long eid)
        {
            string sql = $" update case_physical_evidence_transfere set IsDeleted=1 where EvidenceId={eid}";
            return base.GetDb().Ado.ExecuteCommand(sql) > 0;
        }
    }
}
